<?php
declare (strict_types = 1);
namespace app\index\model\user;

use think\Model;

/**
 * @mixin \think\Model
 */
class UserStationCollection extends Model
{
    protected $pk = "id";

    /**
     * 获取信息
     * @param $where
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getInfoByWhere($where){
        return $this->where($where)->find();
    }

    /**
     * 获取信息
     * @param $id
     * @param $addWhere
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getInfoById($id,$addWhere = [],$field = '*'){
        $where = ['id'=>$id];
        return $this->where($where)->where($addWhere)->field($field)->find();
    }

    /**
     * 获取指定用户的车辆列表
     * @param $userId
     * @param string $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getUserCollection($userId,$field = '*'){
        return $this->where(['user_id'=>$userId])->field($field)
            ->select()->toArray();
    }

    /**
     * 用户收藏站点
     * @param $userId
     * @param $addData
     * @return int|string
     */
    function addUserCollection($userId,$addData){
        $addData = [
            'user_id'=>$userId,
            'station_id'=>$addData['station_id'],
            'create_time'=>date("Y-m-d H:i:s"),
        ];
        return $this->insert($addData);
    }

    /**
     * 删除用户收藏站点
     * @param $where
     * @return bool
     */
    function deleteUserCollection($where){
        return $this->where($where)->delete();
    }

}
